package com.ruoyi.hr.controller.hr.salaryadmin.salaryopencorrection;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.salaryadmin.salaryopencorrection.SalaryOpenCorrectionService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 薪资开启纠错
 */
@Controller
@RequestMapping("/SalaryOpenCorrection/SalaryOpenCorrection")
public class SalaryOpenCorrectionController extends BaseController {

    @Autowired
    private SalaryOpenCorrectionService salaryOpenCorrectionService;

    @Autowired
    AuthorityService authorityService;

    private String prefix = "hr/salaryadmin/salaryopencorrection";

    /**
     * 跳转初始化页面
     */
    @GetMapping()
    public String salaryOpenCorrection() {
        return prefix + "/salaryOpenCorrection";
    }

    /**
     * 跳转初始化页面历史
     */
    @GetMapping("/salaryOpenCorrectionHistory")
    public String salaryChangeCorrectionHistory() {
        return prefix + "/salaryOpenCorrectionHistory";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        startPage();
        return getDataTable(salaryOpenCorrectionService.findData(map, authorityList, isAdmin));
    }

    @RequestMapping("/listHistory")
    @ResponseBody
    public TableDataInfo listHistory(@RequestParam Map map) {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        startPage();
        return getDataTable(salaryOpenCorrectionService.findDataHistory(map, authorityList, isAdmin));
    }

    @GetMapping("/addEmp")
    public String addEmp(@RequestParam Map map, ModelMap mmap) {
        mmap.put("map", map);
        return prefix + "/addEmp";
    }

    @RequestMapping("/cstaffAdd2")
    @ResponseBody
    public BaseResult cstaffAdd2(String allid) {
        return salaryOpenCorrectionService.cstaffAdd2(allid);
    }

    @RequestMapping("/deleteEmp")
    @ResponseBody
    public BaseResult deleteEmp(String allid) {
        return salaryOpenCorrectionService.deleteEmp(allid);
    }

    /**
     * 数据确认,取消确认,执行
     */
    @PostMapping("/threeProcedure")
    @ResponseBody
    public BaseResult threeProcedure(String allId, String type) {
        return salaryOpenCorrectionService.threeProcedure(allId, type);
    }

    @PostMapping("/updateSalary")
    @ResponseBody
    public int updateSalary(@RequestParam Map map) {
        //项目
        String[] array = map.get("benefitTypeAndXl").toString().split("@@@");
        String stdId = array[1];
        map.put("stdId", stdId);
        return salaryOpenCorrectionService.updateSalary(map);
    }
}
